package base2dev.web.pages.musics.crud;

import musics.model.Artist;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.spring.injection.annot.SpringBean;

import base2dev.core.model.Bag;
import base2dev.web.common.MasterPage;
import base2dev.web.pages.musics.ArtistPage;

public class ArtistList extends MasterPage {

	@SpringBean(name = "artists")
	private Bag<Artist> artists;

	public ArtistList() {
		super("ArtistList");
	}

	@Override
	public void setControls() {

		addCreateLink();

		addList();
	}

	private void addList() {

		add(new DataView<Artist>("list", new ListDataProvider<Artist>(
				getArtists().getAll())) {

			private static final long serialVersionUID = 8800917448122220413L;

			@Override
			protected void populateItem(final Item<Artist> item) {
				final Artist a = item.getModelObject();
				item.add(new Label("artist.id", "" + a.getId()));
				
				item.add(new Label("artist.name", a.getName()));
				item.add(new Label("artist.nick", a.getNickName()));
				
				item.add(new Link<Artist>("editLink") {
					
					private static final long serialVersionUID = 7819171384945321404L;

					@Override
					public void onClick() {
						setResponsePage(new ArtistForm(a));
					}
				});

				item.add(new Link<Artist>("deleteLink") {
					private static final long serialVersionUID = -8342006951907263414L;

					@Override
					public void onClick() {
						setResponsePage(new ArtistForm(a));
					}
				});

			}
		});

	}

	private void addCreateLink() {
		add(new Link<Artist>("createLink") {
			private static final long serialVersionUID = 8694050611128212320L;

			@Override
			public void onClick() {
				setResponsePage(ArtistForm.class);
			}
		});
	}

	public void setArtists(Bag<Artist> artists) {
		this.artists = artists;
	}

	public Bag<Artist> getArtists() {
		return artists;
	}

}
